function drawMol(main,colour,GUI)
% This function prints the molecule in either a figure window
% in Commandwindow MODE, or in our custom-built GUI. main contains all
% required data to draw the molecules, whereas colour may be either 1 or
% 0, indicating whether or not the molecule should be printed in colour.
% GUI may be a 1 or 0, indicating GUI usage.%

%% Check additional input and set default.
if ~(exist('colour','var') == 1)
    colour = 1; % Molecule will be drawn with colour by default.
end

% While in Commandwindow MODE/debugmode, a figurewindow is created:
if GUI ~= 1
    figure('Name','SMILES','Color','k'); % Creates a figure with name = SMILES
    set(gca,'Xtick',[],'Ytick',[],'Position',[.00 .00 1.0 1]); % Hides axes, sets position
    whitebg('k') % Black background.
end

%% Drawing the molecule
hold on
for k = 1:length(main)
    %% Drawing the bonds
    for j = 1:length(main(k).bonds)
        if main(k).bonds(j) > k
            bonds = main(k).bondstype(j); % Stores the bond between
            % atom 'k' and 'j' as a multiple of single bonds
            if bonds >= 1
                % Draws a 'single bond'
                line([main(k).x main(main(k).bonds(j)).x], ...
                    [main(k).y main(main(k).bonds(j)).y],...
                    'Color','w','LineStyle','-')
                bonds = bonds - 1; % which leaves 1 less bond to be drawn
                multiBonds(main,bonds,k,j); % Remaining bonds will be
                % drawn by using multiBonds
                
            end
        end
        if strcmp(main(k).element,'.')
            % Using a dirty method, an ionic bond is drawn by drawing a
            % dotted line on top of the previous drawn white line.
            line([main(k).x main(main(k).bonds(j)).x], ...
                [main(k).y main(main(k).bonds(j)).y],...
                'Color','k','LineStyle',':')
        end
    end
end

%% Drawing the atoms
for k = 1:length(main)
    %% Charge
    
    if main(k).charge < 0
        temp = num2str(main(k).charge);
        for l = 1:length(temp)
            charge(l*2) = temp(l);
        end
        for l = 1:length(charge)/2
            charge(l*2-1) = '^';
        end
    elseif main(k).charge > 0
        temp = ['+' num2str(main(k).charge)];
        for l = 1:length(temp)
            charge(l*2) = temp(l);
        end
        for l = 1:length(charge)/2
            charge(l*2-1) = '^';
        end
    else
        charge = '';
    end
    
    %% Drawing
    if colour
        main = MolCol(main); % MolCol adds colours to the atoms, and stores
        % predefined colour in main.
        if ~(strcmp(main(k).element,'.') == 1)
            % The result of the dirty trick, an element . should not be
            % shown on screen!
            at = text(main(k).x,main(k).y,[num2str(main(k).isotope) main(k).element ...
                charge],'HorizontalAlignment','center',...
                'BackgroundColor','k'); % This prints all atom data on the
            % screen
            set(at,'Color',main(k).colour); % and colours it.
        end
    else
        % Same method of printing as above, this time just without colour
        if ~(strcmp(main(k).element,'.') == 1)
            at = text(main(k).x,main(k).y,[num2str(main(k).isotope) main(k).element ...
                charge],'HorizontalAlignment','center',...
                'BackgroundColor','k');
            set(at,'Color','w');
        end
    end
    
    %% Resetting axis
    % As several problems occured when axes weren't specified after each
    % molecule.
    mm = [min(real([main.x])) max(real([main.x]));...
        min(real([main.y])) max(real([main.y]))]; % creates a 2x2 matrix
    % containing min. and max. values
    if mm(1,1) < mm(2,1) % Determines which values to use for axes
        a = 1;
    else
        a = 2;
    end
    if mm(1,2) > mm(2,2)
        b = 1;
    else
        b = 2;
    end
    set(gca,'XLim',[mm(a,1)-1 mm(b,2)+1],'YLim',...
        [mm(a,1)-1 mm(b,2)+1]); % sets axesrange.
end
hold off
end